package com.nightpiter.model.reader.xml.dom;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.graphics.Color;

import com.nightpiter.controller.log.Logger;
import com.nightpiter.exceptions.ModelNotAssignedBuilderException;
import com.nightpiter.exceptions.TimeFormatException;
import com.nightpiter.model.MetroModel;
import com.nightpiter.model.objects.Time;
import com.nightpiter.model.reader.xml.XmlReader;

/**
 * 
 * Realization of the Dom parser
 * 
 * modified by karkaroff (now this class extends XmlReader)
 * 
 * @author Alexandr Melamud
 * 
 */

public class DomReader extends XmlReader {
	/**
	 * public constructor
	 * 
	 * @throws ModelNotAssignedBuilderException
	 */
	public DomReader(MetroModel model) throws ModelNotAssignedBuilderException {
		super(model);
	}

	protected void parse(InputStream inputStream) throws IOException {
		try {
			DomBuilderWrapper builder = new DomBuilderWrapper(this.modelBuilder);

			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			Document document = factory.newDocumentBuilder().parse(inputStream);
			Element map = document.getDocumentElement();

			// ///////////----parsing....the
			// stations....----/////////////////////////////////////

			Element elem = (Element) map.getElementsByTagName(STATIONS).item(0);
			NodeList elem_childs = elem.getElementsByTagName(STATION);
			for (int i = 0; i < elem_childs.getLength(); i++) {

				Element elemStation = (Element) elem_childs.item(i);
				Element elemName = (Element) elemStation.getElementsByTagName(
						NAME).item(0);
				Element elemSchedule = (Element) elemStation
						.getElementsByTagName(SCHEDULE).item(0);
				//Element elemLocation = (Element) elemStation
					//	.getElementsByTagName(LOCATION).item(0);
				Element elemPosition = (Element) elemStation
						.getElementsByTagName(POSITION).item(0);

				Time open = new Time(0);
				try {
					open = new Time(elemSchedule
							.getAttribute(OPEN_ATTR));
				} catch (TimeFormatException e) {
					e.printStackTrace();
				}
				Time close = new Time(0);
				try {
					close = new Time(elemSchedule
							.getAttribute(CLOSE_ATTR));
				} catch (TimeFormatException e) {
					e.printStackTrace();
				}

				/*builder.addStation(Integer.parseInt(elemStation
						.getAttribute(ID_ATTR)), elemName.getChildNodes().item(
						0).getNodeValue(), Integer.parseInt(elemPosition
						.getAttribute(X_ATTR)), Integer.parseInt(elemPosition
						.getAttribute(Y_ATTR)), Integer.parseInt(elemLocation
						.getAttribute(X_ATTR)), Integer.parseInt(elemLocation
						.getAttribute(Y_ATTR)), open, close);*/
				builder.addStation(Integer.parseInt(elemStation
						.getAttribute(ID_ATTR)), elemName.getChildNodes().item(
						0).getNodeValue(), Integer.parseInt(elemPosition
						.getAttribute(X_ATTR)), Integer.parseInt(elemPosition
						.getAttribute(Y_ATTR)), 0, 0, open, close);
			}

			// ///////////----parsing....the
			// tunnels....----/////////////////////////////////////

			elem = (Element) map.getElementsByTagName(TUNNELS).item(0);
			elem_childs = elem.getElementsByTagName(TUNNEL);

			for (int i = 0; i < elem_childs.getLength(); i++) {
				Element elemTunnel = (Element) elem_childs.item(i);
				int time = 0;
				try {
					time = Time.stringToInt(elemTunnel.getAttribute(TIME_ATTR));
				} catch (TimeFormatException e) {
					e.printStackTrace();
				}
				builder.addTunnel(Integer.parseInt(elemTunnel
						.getAttribute(FROM_ATTR)), Integer.parseInt(elemTunnel
						.getAttribute(TO_ATTR)), Integer.parseInt(elemTunnel
						.getAttribute(LINE_ATTR)), time);

			}

			// ///////////----parsing....the
			// lines....----/////////////////////////////////////

			elem = (Element) map.getElementsByTagName(LINES).item(0);
			elem_childs = elem.getElementsByTagName(LINE);

			for (int i = 0; i < elem_childs.getLength(); i++) {

				Element elemLine = (Element) elem_childs.item(i);

				Element elemName = (Element) elemLine
						.getElementsByTagName(NAME).item(0);
				builder.addLine(Integer
						.parseInt(elemLine.getAttribute(ID_ATTR)), elemName
						.getChildNodes().item(0).getNodeValue(), Color
						.parseColor(elemLine.getAttribute(COLOR_ATTR)));
			}

			// ///////////----parsing....the
			// transfers....----/////////////////////////////////////

			elem = (Element) map.getElementsByTagName(TRANSFERS).item(0);
			elem_childs = elem.getElementsByTagName(TRANSFER);

			for (int i = 0; i < elem_childs.getLength(); i++) {
				Element elemTransfer = (Element) elem_childs.item(i);
				int time = 0;
				try {
					time = Time.stringToInt(elemTransfer
							.getAttribute(TIME_ATTR));
				} catch (TimeFormatException e) {
					e.printStackTrace();
				}
				builder.addTransfer(Integer.parseInt(elemTransfer
						.getAttribute(FROM_ATTR)), Integer
						.parseInt(elemTransfer.getAttribute(TO_ATTR)), time);

			}

			// ///////////----parsing....finishing....----/////////////////////////////////////

		} catch (SAXException e) {
			Logger.logException(e);
		} catch (IOException e) {
			Logger.logException(e);
		} catch (ParserConfigurationException e) {
			Logger.logException(e);
		}

	}

}
